<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use app\service\apiService;
use think\facade\Db;
use think\facade\View;

class Community extends Common{
    //查询小区信息
    public function communitylist(){
        $com = $this->pidTokenId();
        //$where['id'] = array('in',$com['community_id']);
        //查询小区信息
        $where=[];
        $where[] =["id","in",$com['community_id']];
        $list = (new communityService())->communitylist($where);
        //var_dump($list);exit;
        View::assign('list', $list);
        //查询数据条数
        $number =Db::table("community")->where($where)->count();
        View::assign('number', $number);
        return View::fetch('community_list');
    }
    //添加小区信息
    public function communityAdd(){
        //根据当前账号查询物业信息
        $com = $this->pidTokenId();
        //echo "<pre>";
        //var_dump($com);die;
        $where['id'] = array('in',$com['property_id']);
       
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断小区是否重复
            $info = (new communityService())->communityName($data['name']);
            if($info){
                $this->error('项目已添加');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //将数据插入数据库
            $result = (new communityService())->communityAdd($data);
            //判断数据是否插入成功
            if($result){
                $this->success('项目添加成功',url('communitylist'));
            }else{
                $this->error('项目添加失败',url('communityAdd'));
            }
        }else{
            return View::fetch('community_add');
        }
    }

    //修改物业小区信息
    public function communityEdit() {
        //查询物业支付宝账号和token
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);
        $propertylist =(new communityService())->propertyList($where);
        View::assign('propertylist',$propertylist);
        if (request()->isGet()) {
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('小区不存在');
            }
            $info = (new communityService())->communityIdInfo($id);
            View::assign('info', $info);
            return View::fetch('community_edit');
        }else{
            $data = request()->post();
			//print_r($data);
			//die;
            $result = (new communityService())->communityEdit($data);
            if($result) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    //添加小区打印机
    public function printing(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('小区不存在');
            }
            $info = (new communityService())->communityIdInfo($id);
            View::assign('info',$info);
            if($info){
                return View::fetch('community_printing');
            }else{
                $this->error('小区信息错误');
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new communityService())->communityEdit($data);
            //判断数据是否插入成功
            if($result){
                $this->success('配置打印机成功',url('communitylist'));
            }else{
                $this->error('配置打印机失败',url('communitylist'));
            }
        }
    }
    
    public function communityDel(){
        $id = request()->param('id');
        $validate = $this->validate(request()->param(),[
            'id'    =>  'require|number'
        ]);
        if($validate !== true){
            $this->error($validate);
        }
        //判断是否存在该数据
        $info = (new communityService())->communityIdInfo($id);
        if(empty($info)){
            $this->error('该小区不存在');
        }
        $result = Db::name('community')->delete($id);
        if($result){
            $this->success('删除成功',url('communityList'));
        }else{
            $this->success('删除失败',url('communityList'));
        }
    }
}